
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px" v-loading="loading">
    <el-form-item label="项目名称" prop="title">
        <el-input v-model.trim="data.title" placeholder="请填写项目名称" />
           </el-form-item>
        <el-form-item label="所属客户" prop="cid">
          <el-select v-model.trim="data.cid" placeholder="请选择所属客户" clearable  class="filter-item" >
            <el-option  v-for="(customer,index) in customer_list" :key="index" :value="customer.id" :label="customer.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同照片" prop="contract_img" >
            <el-upload
                    class="avatar-uploader"
                    v-loading="data.contract_img_loading"
                    element-loading-text="图片上传中"
                    :action="$store.state.settings.file_upload_url"
                    :headers ="headerObj"
                    :show-file-list="false"
                    accept="image/png, image/jpeg, image/gif, image/jpg"
                    :on-success="handlecontract_imgSuccess"
                    :before-upload="beforecontract_imgUpload">
                        <img v-if="data.contract_img" :src="data.contract_img" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>
          <el-form-item label="服务周期">
                <el-date-picker type="daterange" value-format="yyyy-MM-dd" placeholder="请选择日期" start-placeholder="开始日期" end-placeholder="结束日期" range-separator="/" v-model="data.service_date"></el-date-picker>
          </el-form-item>
          <el-form-item label="服务期数" prop="service_periods">
              <el-input-number v-model="data.service_periods" :min="1"  label="服务期数"></el-input-number>
          </el-form-item>
          <el-form-item label="项目负责人" prop="admin_id">
            <el-select v-model.trim="data.admin_id" placeholder="请选择项目负责人" clearable  class="filter-item" >
              <el-option  v-for="(admin,index) in admin_list" :key="index" :value="admin.id" :label="admin.nickname"></el-option>
          </el-select>
          </el-form-item>
  
          <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
              <img width="100%" :src="previewUrl" alt="">
          </el-dialog>  
    <el-form-item>
      <el-button type="primary" icon="el-icon-check" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {detail,edit} from '@/api/projects'
import { getToken } from '@/utils/auth'



export default {
  name: 'projectsEdit',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    customer_list: {
      type: Array,
      default: () => {
        return []
      }
    },
    admin_list: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      loading:true,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
        'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    title: [ 
                { required: true, message: "请输入项目名称", trigger: "blur" }
            ], 
            cid: [ 
                { required: true, message: "请输入所属客户", trigger: "blur" }
            ], 
            service_date: [ 
                { required: true, message: "请输入服务周期", trigger: "blur" }
            ], 
            admin_id: [ 
                { required: true, message: "请输入项目负责人", trigger: "blur" }
            ], 

      },
      data: {
            title:'',
            cid:'',
            contract_img:'',
            contract_img_loading:false,
            service_date:[],
            service_periods:'',
            admin_id:null,

         }
    }
  },
  created() {
    const id = this.item.id
    this.id = id
    this.fetchData(id)
  },
  watch: {
    item: {
      handler(val, oldVal) {
        if (this.item.dialogVisible === true 
        ) {
          this.id = val.id
          this.fetchData(val.id)
        }
      },
      deep: true
    }
  },
  methods: {
             handlecontract_imgSuccess(response, file, fileList) {
                this.$store.commit('settings/SET_AJAX_BUTTON', false);
                this.data.contract_img_loading = false 
                     if(response.data.code == 0){
                         this.data.contract_imgfileList = []
                     return this.$message.error(response.msg);
            }
                this.data.contract_img= response.data.src;
            },
             beforecontract_imgUpload(file) {
                this.data.contract_img_loading = true 
                this.$store.commit('settings/SET_AJAX_BUTTON', true);
                const isJPG = file.type === "image/jpeg";
                const isPNG = file.type === "image/png";
                if (!isJPG && !isPNG) {
                 return this.$message.error("上传头像图片只能是 JPG 、PNG格式!");
                }
            },

    fetchData(id) {
          detail(id).then(response => {
            this.loading = false;
            this.data = response.data;
                        this.data.service_date=response.data.service_date.split('/');

          }).catch(err => {
            console.log(err)
          })
        },
    submit() {
      const thiz = this
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        edit({
          id: this.data.id,
                        title:this.data.title,
            cid:this.data.cid,
            contract_img:this.data.contract_img,
            service_date:this.data.service_date.join('/'),
            service_periods:this.data.service_periods,
            admin_id:this.data.admin_id,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '编辑成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>
